package com.util;

import com.ATMgui.Users;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Util {
    public static Image getImage(String path) {
        return new ImageIcon(Util.class.getResource(path)).getImage();
    }

    public static Icon getIcon(String path) {
        return new ImageIcon(Util.class.getResource(path));
    }

    /*
创建数据库连接
     */
    public static Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "997424586qwE!");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    /*
    查询
     */
    public static ArrayList query() {
        //创建连接
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        ArrayList<Users> usertList = new ArrayList();
        try {
            statement = con.createStatement();
            String sql = "select  u_name,u_password,u_blance from t_users";
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                Users users = new Users();
                users.setBlance(rs.getDouble("u_blance"));
                users.setPassWord(rs.getString("u_password"));
                users.setUserName(rs.getString("u_name"));
                usertList.add(users);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return usertList;
    }

    /*
    添加
     */
    public static void insert(String sql) {
        //创建连接
        Connection con = getConnection();
        Statement statement = null;
        try {
            //获取执行SQL语句对象
            statement = con.createStatement();
            int row = statement.executeUpdate(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    public static void close(Connection con, Statement statement) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static void close(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
